package com.activities;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.clases.Lugar;
import com.constantes.ItemInicio;
import com.example.santiagotur.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;


public class Imagen_list_inicio implements ItemInicio {

    private final int ColumnWidth;

    private final LayoutInflater inflater;

    private final Lugar lugar;

    class ViewHolder {

        public ImageView imagen_lugares_inicio;

        public ProgressBar progreso;

        public TextView texto_name_lugares_inicio;

    }

    public Imagen_list_inicio(LayoutInflater inflater, Lugar lugar,
                int ColumnWidth) {

        super();
        this.inflater = inflater;
        this.lugar = lugar;
        this.ColumnWidth = ColumnWidth;
    }

    /*
     * (non-Javadoc)
     * @see com.constantes.ItemInicio#getType()
     */
    @Override
    public int getType() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.constantes.ItemInicio#getView()
     */
    @Override
    public View getView(View view) {

        ViewHolder holder = null;

        if (view == null) {

            view = inflater.inflate(R.layout.gridview, null);
            holder = new ViewHolder();
            //
            holder.imagen_lugares_inicio = (ImageView) view.findViewById(R.id.thumb_lugar);
            holder.texto_name_lugares_inicio = (TextView) view.findViewById(R.id.name_lugar_indivi);
            holder.progreso = (ProgressBar) view.findViewById(R.id.loading_progress);
            holder.imagen_lugares_inicio.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.imagen_lugares_inicio.setLayoutParams(new RelativeLayout.LayoutParams(this.ColumnWidth, this.ColumnWidth));
            //
            view.setTag(holder);

        }
        else {

            holder = (ViewHolder) view.getTag();
        }

        holder.imagen_lugares_inicio.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                click(v.getContext());

            }
        });

        setearGrid(holder);

        return view;
    }

    private void click(Context v) {

        Intent intentClick = new Intent(v, Principal_Lugar.class);
        Principal_Lugar.lugar = lugar;
        v.startActivity(intentClick);

    }

    private void descargarImagen(ImageView imagen, String Url, final ProgressBar progressbar) {

        ImageLoader img = ImageLoader.getInstance();

        img.displayImage(Url, imagen, getOptionsForGrid(), new ImageLoadingListener() {

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                progressbar.setVisibility(View.GONE);

                if (loadedImage != null) {

                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                progressbar.setVisibility(View.GONE);

            }

            @Override
            public void onLoadingStarted(String imageUri, View view) {

                progressbar.setVisibility(View.VISIBLE);

            }

        });

    }

    private DisplayImageOptions getOptionsForGrid() {

        return new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).considerExifParams(false).imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).bitmapConfig(Bitmap.Config.RGB_565).build();
    }

    private void setearGrid(ViewHolder holder) {

        holder.texto_name_lugares_inicio.setText(lugar.getNombre());
        descargarImagen(holder.imagen_lugares_inicio, lugar.getPortada(), holder.progreso);
    }

}
